// miniprogram/pages/replacemodify/replace3_ss/ss.js
const db=wx.cloud.database();
const _ = db.command;
//时间转换函数
import common from "../../../utils/getMyData.js";

Page({

  /**
   * 页面的初始数据
   */
  data: {
     //下拉框显示
     getPatientIdToSs:"",
     ss_value:"",  //自动输入需要确定的value
     ss_bed:"",ss_sex:"",ss_age:"",ss_situation:"",//需要自动输入的值

     option2: [
      { text: '病人姓名', value: 0 },
      ],
      value2: 0,

    //特殊交班页面
    ss_temper_top:"",
    ss_temper_down:"",
    ss_rate_top:"",
    ss_rate_down:"",
    ss_press_top:"",
    ss_press_down:"",
    ss_blood_oxygen:"",
    ss_mind:"",
    ss_vision:"",
    ss_touch:"",
    ss_knock:"",
    ss_hear:"",
    ss_examine:"",
    ss_yester_desc:""
  },

  //输入框回调
  onChange(e) {
    //输入框
    console.log(e.detail);   
    this.setData({
      //因为按钮处要进行wx:if判断，所以要将所有输入框的数据声明
      [e.target.dataset.key]:e.detail
    })
    console.log(this.data)
  },

  //提交表单添加进数据库
  btnSub_ss(res){
    var {ss_temper_top,ss_temper_down,ss_rate_top,ss_rate_down,ss_press_top,ss_press_down,ss_blood_oxygen,ss_mind,ss_vision,ss_touch,ss_knock,ss_hear,ss_examine,ss_yester_desc,ss_patient_id}=res.detail.value
    //先判断选择患者是不是‘病人姓名‘ 
    if(ao_patient_id!=0){ 
    
      //进入加载页面
      wx.showLoading({
        title: '加载中...',
        mask: true
        })
        setTimeout(function () {
          wx.hideLoading()
        }, 2000)
      wx.cloud.callFunction({
        name: 'login'
      }).then(res => {
        console.log(res.result.openid)
        wx.cloud.callFunction({
          name:"newAdd",
          data:{
            action:'ss',
            ss_openid:res.result.openid,
            patient_id:this.data.getPatientIdToSs[ss_patient_id-1].patient_id, 
            ss_temper_top:ss_temper_top,
            ss_temper_down:ss_temper_down,
            ss_rate_top:ss_rate_top,
            ss_rate_down:ss_rate_down,
            ss_press_top:ss_press_top,
            ss_press_down:ss_press_down,
            ss_blood_oxygen:ss_blood_oxygen,
            ss_mind:ss_mind,
            ss_vision:ss_vision,
            ss_touch:ss_touch,
            ss_knock:ss_knock,
            ss_hear:ss_hear,
            ss_examine:ss_examine,
            ss_yester_desc:ss_yester_desc,
            database_id:this.data.database_id
          }
        })
      }).then(res=>{
        console.log(res)
        //清除填写内容
        this.setData({
          //清空value
          // value2:0,
          // ss_bed:"",
          // ss_sex:"",
          // ss_age:"",
          // ss_situation:"",
          // value:"",
          // message:"",
          //清空值
          // ss_temper_top:"",
          // ss_temper_down:"",
          // ss_rate_top:"",
          // ss_rate_down:"",
          // ss_press_top:"",
          // ss_press_down:"",
          // ss_blood_oxygen:"",
          // ss_mind:"",
          // ss_vision:"",
          // ss_touch:"",
          // ss_knock:"",
          // ss_hear:"",
          // ss_examine:"",
          // ss_yester_desc:""
        })
      })
    }
  },
  
  //从patient数据库获取病人姓名给特殊交班并自动匹配输入相关数据(该数据只需取不需填入数据库)
  //从patient数据库获取病人姓名给特殊交班并自动匹配输入相关数据(该数据只需取不需填入数据库)
  getPatientToSs(event){
    db.collection("patient").where({
      patient_del:false
    }).get().then(res=>{
      this.setData({
        getPatientIdToSs:res.data
      })
      for(var i=0;i<res.data.length;i++){
        this.data.option2[i+1]={ text: res.data[i].patient_name, value: i+1 }
      }
      this.setData({
        option2:this.data.option2,
      })
    })
  },
  //获取value并以此自动输入床号等
  getValue(res){
    if(res.detail==0){
      this.setData({
        value2:res.detail,
        ss_value:res.detail,
        ss_bed:"",
        ss_sex:"",
        ss_age:"",
        ss_situation:""
      })
    }

    //判断当前时间和手术时间，给ss_situation赋值
    this.data.ss_situation=""     //先将ss_situation清零
    if(res.detail!=0){
      for(var i=0;i<this.data.getPatientIdToSs[res.detail-1].patient_oper_list.length;i++){
        this.data.ss_situation=this.data.ss_situation+this.data.getPatientIdToSs[res.detail-1].patient_oper_list[i].name
        if(new Date().getTime()>=this.data.getPatientIdToSs[res.detail-1].patient_oper_list[i].time){
          this.data.ss_situation=this.data.ss_situation+"术后  "
        }else{
          this.data.ss_situation=this.data.ss_situation+"术前  "
        }
      }
      if(this.data.getPatientIdToSs[res.detail-1].patient_oper_list.length==0){
        this.data.ss_situation=this.data.ss_situation+"无"
      }
      this.setData({
        value2:res.detail,
        ss_value:res.detail,
        ss_bed:this.data.getPatientIdToSs[res.detail-1].patient_bed,
        ss_sex:this.data.getPatientIdToSs[res.detail-1].patient_sex,
        ss_age:this.data.getPatientIdToSs[res.detail-1].patient_age,
        ss_situation:this.data.ss_situation
      })
    }
  }, 
  //在点开的时候将value的值设为-1，以触发bind:change
  // changeValue(){
  //   this.setData({
  //     value2:-1
  //   })
  // },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    //获取患者数据 
    wx.cloud.callFunction({
      name: 'newModify', 
      data:({ 
        action:'ss_get',
        id: this.options.id.toString()
      }) 
   }).then(res => {
      this.setData({ 
       database_id: this.options.id.toString(),
       record: res.result.data[0],
       
       ss_temper_top:res.result.data[0].ss_feature.temper_top,
       ss_temper_down:res.result.data[0].ss_feature.temper_down,
       ss_rate_top:res.result.data[0].ss_feature.rate_top,
       ss_rate_down:res.result.data[0].ss_feature.rate_down,
       ss_press_top:res.result.data[0].ss_feature.temper_top,
       ss_press_down:res.result.data[0].ss_feature.press_down,
       ss_blood_oxygen:res.result.data[0].ss_feature.blood_oxygen,
       ss_mind:res.result.data[0].ss_feature.mind,
       ss_vision:res.result.data[0].ss_feature.belly_feat.vision,
       ss_touch:res.result.data[0].ss_feature.belly_feat.touch,
       ss_knock:res.result.data[0].ss_feature.belly_feat.knock,
       ss_hear:res.result.data[0].ss_feature.belly_feat.hear,
       ss_examine:res.result.data[0].ss_examine,
       ss_yester_desc:res.result.data[0].ss_yester_desc,
      }) 
      console.log(res.result.data)
      console.log(this.options)
      //获取数据 
      wx.hideLoading() 
   })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})